#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <signal.h>
#include <string.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/reboot.h>
#include <sys/types.h>
#include <dirent.h>

static int rm_dir(const char *path)
{
    struct dirent *dir;
    struct stat st;
    char sub_path[64];

    DIR* dirp = opendir(path);
    if(!dirp) {
        return -1;
    }

    while((dir = readdir(dirp)) != NULL) {
        if(strcmp(dir->d_name,".") == 0
                || strcmp(dir->d_name,"..") == 0) {
            continue;
        }
        sprintf(sub_path, "%s/%s", path, dir->d_name);
        if(lstat(sub_path,&st) == -1) {
            continue;
        } if (S_ISDIR(st.st_mode)) {
            if(rm_dir(sub_path) == -1) {
                closedir(dirp);
                return -1;
            }
            rmdir(sub_path);
        } else if (S_ISREG(st.st_mode)) {
            unlink(sub_path);
        } else {
            continue;
        }
    }
    if(rmdir(path) == -1) {
        closedir(dirp);
        return -1;
    }

    closedir(dirp);
    return 0;
}

int main()
{
    rm_dir("abc/bbc");
    return 0;
}
